package com.hsc.www;

import org.springframework.cache.interceptor.KeyGenerator;
import java.lang.reflect.Method;

public class CustomCacheKeyGenerator implements KeyGenerator {

    @Override
    public Object generate(Object target, Method method, Object... params) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(target.getClass().getName());
        addMethod(method, stringBuilder);
        if (params != null) {
            stringBuilder.append(":").append(generateKey(params).toString());
        }
        return stringBuilder.toString();
    }

    void addMethod(Method method, StringBuilder sb) {
        sb.append(":").append(method.getName()).append("(");
        Class<?>[] types = method.getParameterTypes();
        for (int j = 0; j < types.length; j++) {
            sb.append(types[j].getTypeName());
            if (j < (types.length - 1))
                sb.append(",");
        }
        sb.append(")");
    }

    public static Object generateKey(Object... params) {
        return new CustomCacheKey(params);
    }


}
